package com.longcai.service.impl.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.customer.ClyjUserAgent;
import com.longcai.entity.sys.SysUser;
import com.longcai.mapper.SysRoleMapper;
import com.longcai.mapper.SysRoleUserMapper;
import com.longcai.mapper.SysUserMapper;
import com.longcai.mapper.clyjuser.ClyjUserAgentMapper;
import com.longcai.mapper.clyjuser.ClyjUserMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjUserAgentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 用户代理表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-05-10
 */
@Service
public class ClyjUserAgentServiceImpl extends ServiceImpl<ClyjUserAgentMapper, ClyjUserAgent> implements ClyjUserAgentService {

    @Autowired
    private ClyjUserAgentMapper clyjUserAgentMapper;
    @Autowired
    private ClyjUserMapper clyjUserMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取代理商列表
     *
     * @param clyjUserAgent
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse agentList(ClyjUserAgent clyjUserAgent, PageHelper pageHelper) {
        try {
            //分页
            Page<ClyjUserAgent> page = new Page<ClyjUserAgent>(pageHelper.getStart(), pageHelper.getLength());
            QueryWrapper<ClyjUserAgent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 0);
            if (!StringUtils.isEmpty(clyjUserAgent.getPhone()) && !clyjUserAgent.getPhone().equals("")) {
                queryWrapper.like("phone", clyjUserAgent.getPhone());
            }
            if (!StringUtils.isEmpty(clyjUserAgent.getS_city()) && !clyjUserAgent.getS_city().equals("地级市")) {
                queryWrapper.like("city", clyjUserAgent.getS_city());
            }
            if (!StringUtils.isEmpty(clyjUserAgent.getS_county()) && !clyjUserAgent.getS_county().equals("区、县级市")) {
                queryWrapper.like("county", clyjUserAgent.getS_county());
            }
            IPage<ClyjUserAgent> clyjUserAgentIPage = this.clyjUserAgentMapper.selectPage(page, queryWrapper);
            PageTableResponse pageTableResponse = new PageTableResponse(clyjUserAgentIPage.getTotal(),
                    clyjUserAgentIPage.getTotal(), clyjUserAgentIPage.getRecords());
            return pageTableResponse;
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
        return null;
    }

    /**
     * 根据id获取代理商信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getById(String id) {
        try {
            ClyjUserAgent clyjUserAgent = this.clyjUserAgentMapper.selectById(id);
            logger.debug("查询成功");
            return ApiResult.success(clyjUserAgent);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询失败");
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult delById(String id) {
        try {
            ClyjUserAgent clyjUserAgent = this.clyjUserAgentMapper.selectById(id);
            clyjUserAgent.setStatus(99);
            logger.debug("删除成功");
            return ApiResult.success("删除成功" + clyjUserAgent);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除失败");
            return ApiResult.fail("删除失败" + e.getMessage());
        }
    }

    /**
     * 新增代理
     *
     * @param clyjUserAgent
     * @return
     */
    @Override
    public ApiResult saveagent(ClyjUserAgent clyjUserAgent) {
        try {
            ClyjUserAgent clyjUserAgent1 = clyjUserAgent;
            //根据手机号查询用户id
            QueryWrapper<ClyjUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phone", clyjUserAgent1.getPhone());
            ClyjUser clyjUser = this.clyjUserMapper.selectOne(queryWrapper);
            clyjUserAgent1.setId(UUID.randomUUID().toString().replace("-", ""));
            if (clyjUserAgent1.getAgentFlag() == 1) {
                clyjUserAgent1.setCity(clyjUserAgent1.getS_city());
            } else if (clyjUserAgent1.getAgentFlag() == 2) {
                clyjUserAgent1.setCounty(clyjUserAgent1.getS_county());
            }
            clyjUserAgent1.setStatus(0);
            clyjUserAgent1.setUserId(clyjUser.getUserId());
            clyjUserAgent1.setCreateTime(new Date());
            clyjUserAgent1.setUpdateTime(new Date());
            Integer integer = this.clyjUserAgentMapper.insert(clyjUserAgent1);
            //把代理商信息存入登录用户表
            SysUser sysUser = new SysUser();
            sysUser.setId(Integer.parseInt(clyjUser.getUserId()));
            sysUser.setUsername(clyjUser.getPhone());
            sysUser.setPassword(clyjUser.getPassword());
            logger.debug("新增成功");
            sysUserMapper.insert(sysUser);
            return ApiResult.success(integer);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("新增失败");
            return ApiResult.fail("新增失败" + e.getMessage());
        }
    }

    /**
     * 修改代理
     *
     * @param clyjUserAgent
     * @return
     */
    @Override
    public ApiResult updatebyagent(ClyjUserAgent clyjUserAgent) {
        try {
            ClyjUserAgent clyjUserAgent1 = clyjUserAgent;
            clyjUserAgent1.setUpdateTime(new Date());
            if (clyjUserAgent1.getAgentFlag() == 1) {
                clyjUserAgent1.setCity(clyjUserAgent1.getS_city());
            } else if (clyjUserAgent1.getAgentFlag() == 2) {
                clyjUserAgent1.setCounty(clyjUserAgent1.getS_county());
            }
            Integer integer = this.clyjUserAgentMapper.updateById(clyjUserAgent1);
            logger.debug("修改成功");
            return ApiResult.success(integer);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改失败");
            return ApiResult.fail("修改失败" + e.getMessage());
        }
    }


}
